const pool = require('./db.js')
class favoritesDao {
  // favoritesDao.js
  async getByUserId(userId) {
    const [rows] = await pool.query(`
    SELECT f.*, a.title, a.content 
    FROM favorites f
    JOIN articles a ON f.article_id = a.id
    WHERE f.user_id = ?
  `, [userId]);
    return rows;
  }

  // 根据文章的id新增用户收藏
  async addFavorites(article_id, user_id) {
    try {
      let result = await pool.execute('insert favorites(article_id,user_id) values(?,?)', [article_id, user_id])
      return result;
    } catch (err) {
      console.log('出错:' + err)
    }
  }
}

module.exports = new favoritesDao()